package com.molyfun.parents.activity.organization;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.SplashActivity;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.OrganizationListResponse;
import com.molyfun.parents.utils.ActivityManager;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.view.TwoTabBar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:04/05/17.
 */
public class OrganizationChooseDialogActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.organization_list)
    RecyclerView mRecyclerView;
    @InjectView(R.id.organization_dialog_layout)
    LinearLayout mOrganizationDialogLayout;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.organization_enter)
    TextView mEnterOrganization;
//    @InjectView(R.id.close)
//    ImageView mClose;

    private LinearLayoutManager mLayoutManager;
    private OrganizationListAdapter mOrganizationListAdapter;

    private static boolean isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_organization_choose_dialog);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");

        mEnterOrganization.setOnClickListener(this);

//        mClose.setOnClickListener(this);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mOrganizationListAdapter = new OrganizationListAdapter(this);
        mRecyclerView.setAdapter(mOrganizationListAdapter);

    }

    public static void startActivity(Context context){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(context, OrganizationChooseDialogActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if(!PreferencesStorageUtil.isOriganOutShowSplash(MFPApplication.getInstance())){
        if(!isShow){
            getOrganizationList();
            isShow = true;
        }else{
            finish();
        }
//        }else{
//            finish();
//        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        isShow = false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.organization_enter:
                if(mOrganizationListAdapter.getChoosedOrganization() != null){
                    changeOrganization(mOrganizationListAdapter.getChoosedOrganization().getId(), mOrganizationListAdapter.getChoosedOrganization().getStudentid());
                }
                break;
//            case R.id.close:
//                Intent intent = new Intent(MFPApplication.getInstance(), SplashActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                MFPApplication.getInstance().startActivity(intent);
//                PreferencesStorageUtil.setOriganOutShowSplash(MFPApplication.getInstance(), true);
//                ActivityManager.getInstance().finishMainActivity();
//                finish();
//                break;
        }
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }

    private void getOrganizationList() {
        showProgressDialog();

        Call<OrganizationListResponse> call = MFPApplication.getInstance().getRestClient().getOrganizationList( "pass", RestClient.DEFAULT_PAGE, RestClient.LARGE_PAGE_SIZE);
        call.enqueue(new Callback<OrganizationListResponse>() {
            @Override
            public void onResponse(Call<OrganizationListResponse> call, Response<OrganizationListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getData() != null) {
                        if (response.body().getData().size() > 0) {
//                            if(mTwoTabBar.getCurrentChooseTab() == TwoTabBar.TwoTabChooseTab.BAR_ONE){
//                                showInviteOption = false;
//                            }
                            mOrganizationDialogLayout.setVisibility(View.VISIBLE);
                            mOrganizationListAdapter.refreshData(response.body().getData(), TwoTabBar.TwoTabChooseTab.BAR_ONE);
                            mEmptyLayout.setVisibility(View.GONE);
                        } else {
                            if(!PreferencesStorageUtil.isOriganOutShowSplash(MFPApplication.getInstance())){
                                Intent intent = new Intent(MFPApplication.getInstance(), SplashActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                MFPApplication.getInstance().startActivity(intent);
                                PreferencesStorageUtil.setOriganOutShowSplash(MFPApplication.getInstance(), true);
                                ActivityManager.getInstance().finishMainActivity();
                            }
                            finish();
                        }
                }else{
                    if(!PreferencesStorageUtil.isOriganOutShowSplash(MFPApplication.getInstance())){
                        Intent intent = new Intent(MFPApplication.getInstance(), SplashActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        MFPApplication.getInstance().startActivity(intent);
                        PreferencesStorageUtil.setOriganOutShowSplash(MFPApplication.getInstance(), true);
                        ActivityManager.getInstance().finishMainActivity();
                    }
                    finish();
                }
            }

            @Override
            public void onFailure(Call<OrganizationListResponse> call, Throwable t) {
                mOrganizationListAdapter.clearAll();
                mEmptyLayout.setVisibility(View.VISIBLE);
                dismissProgressDialog();
            }
        });
    }

    private void changeOrganization(String departId, String studentId){
        showProgressDialog();

        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().changeOrganization(departId, studentId);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getCode() == 200) {
                    toast("切换机构成功");
                    Intent intent = new Intent(MFPApplication.getInstance(), SplashActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    MFPApplication.getInstance().startActivity(intent);
                    ActivityManager.getInstance().finishMainActivity();
                    finish();
                }else{
//                    dismissProgressDialog();
                    toast("切换失败");
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                toast("切换失败");
                dismissProgressDialog();
            }
        });
    }

}
